#include "Camera.h"
#include <iostream>

void Camera::Update()
{
	view = XMMatrixLookToLH(pos, dirs[2], dirs[1]);
	proj = XMMatrixPerspectiveFovLH(fov, aspect, nearClip, farClip);
}

void Camera::Rotate(AXIS axis, float angle)
{
	XMMATRIX rot;
	switch(axis){
	case AXIS_X:
		rot = XMMatrixRotationAxis(dirs[AXIS_X], angle);
		dirs[AXIS_Y] = XMVector3TransformNormal(dirs[AXIS_Y], rot);
		dirs[AXIS_Z] = XMVector3TransformNormal(dirs[AXIS_Z], rot);
		break;
	case AXIS_Y:
		rot = XMMatrixRotationAxis(XMVectorSet(0, 1, 0, 0), angle);
		for(auto& d : dirs) d = XMVector3TransformNormal(d, rot);
		break;
	case AXIS_Z:
		break;
	default:
		break;
	}
}

void Camera::Move(AXIS axis, float distance)
{
	pos += ( dirs[axis] * distance );
}

Camera::Camera(const XMFLOAT3& position, const XMFLOAT3& target, const XMFLOAT3& up, float fov, float nearClip, float farClip)
{
	pos = XMLoadFloat3(&position);
	auto tar = XMLoadFloat3(&target);
	dirs[AXIS_Z] = XMVector3Normalize(tar-pos);
	dirs[AXIS_Y] = XMLoadFloat3(&up);
	dirs[AXIS_X] = XMVector3Normalize(XMVector3Cross(dirs[AXIS_Y], dirs[AXIS_Z]));
	dirs[AXIS_Y] = XMVector3Normalize(XMVector3Cross(dirs[AXIS_Z], dirs[AXIS_X]));
	this->fov = fov;
	this->nearClip = nearClip;
	this->farClip = farClip;
}
